package com.example.onion.hxchat.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import com.example.onion.hxchat.activity.MsgTestActivity;
import com.example.onion.hxchat.adapter.MyMsgAdapter;
import com.example.databean.Msg;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

import static androidx.constraintlayout.widget.Constraints.TAG;

// 不用了
public class ChatContentFragment extends Fragment {

    private View view;
    private RecyclerView mRecyclerView;
    private MyMsgAdapter myMsgAdapter;
    private List<Msg> mList = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private DataGetterListener mDataGetterListener;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.chat_content, container, false);
        swipeRefreshLayout = view.findViewById(R.id.fragment_chat_swiperefreshlayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //此处给出聊天界面刷新的逻辑代码
                Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        initRecyclerView();
        return view;
    }


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach is invoke");
        mList = ((MsgTestActivity) context).getMsgList();//通过强转成宿主activity，就可以获取到传递过来的数据
//        Log.d(TAG, "onAttach is invoke");
//        String titles = ((MsgTestActivity) context).getTitles();//通过强转成宿主activity，就可以获取到传递过来的数据
//        Log.d(TAG, "onAttach is invoke context "+titles);
//        String titles1 = ((MsgTestActivity)getActivity()).getTitles();//两种方法
//        Log.d(TAG, "onAttach is invoke getActivity "+titles1);


    }


    /**
     * TODO 对recycleview进行配置
     */

    private void initRecyclerView() {
        //获取RecyclerView
        mRecyclerView = view.findViewById(R.id.fragment_chat_recycler_view);
        //创建adapter
        myMsgAdapter = new MyMsgAdapter(mList, getActivity());
        //给RecyclerView设置adapter
        mRecyclerView.setAdapter(myMsgAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        //设置item的分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
//        myFriendAdapter.setOnItemClickListener(new CollectRecycleAdapter.OnItemClickListener() {
//            @Override
//            public void OnItemClick(View view, GoodsEntity data) {
//                //此处进行监听事件的业务处理
//                Toast.makeText(getActivity(),"我是item",Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(Msg msg);//将本页面的获得的infoBean数据通过接口回调给调用方
    }

    public void setDataGetterListener(DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }

    public void refreshData() {
        myMsgAdapter.notifyItemInserted(mList.size() - 1); // 当有新消息时，刷新ListView中的显示
        mRecyclerView.scrollToPosition(mList.size() - 1); // 将ListView定位到最后一行
    }

    /**
     * 保证输入框不遮挡聊天界面
     */
    public void ensureShowContent()
    {
        mRecyclerView.scrollToPosition(mList.size() - 1); // 将ListView定位到最后一行
    }
}
